
package com.example.filetransfer;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;

import com.example.filetransfer.FileTransferService.MyBinder;
import com.example.filetransfer.publicsource.connection.DataReceiver.onReceiveMessageListener;
import com.example.filetransfer.publicsource.connection.FileTransferListener;

public class MainActivity extends Activity implements FileTransferListener,
        onReceiveMessageListener {
    private FileTransferService mFileTransferService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mFileTransferService != null) {
                    mFileTransferService.doWork(FileTransferService.SYNC_FROM_PC_ACTION);
                }
            }
        });
    }

    @Override
    protected void onStart() {
        Intent intent = new Intent("com.yadong.test.syncfrompc");
        bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
        super.onStart();
    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mFileTransferService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mFileTransferService = ((MyBinder) service).getService();
            mFileTransferService.setFileTransferListener(MainActivity.this);
            mFileTransferService.setReceiveMessageListener(MainActivity.this);
        }
    };

    protected void onStop() {
        super.onStop();
    };

    @Override
    public void receiveMessage(String message) {
        ((MyProgressBar) findViewById(R.id.button)).setText(message);
    }

    @Override
    public void onProgressChanged(String fileUrl, int progress) {
        ((MyProgressBar) findViewById(R.id.button)).setProgress(progress);
    }

}
